package com.yvan.leto.server.service;

import com.yvan.leto.SegmentHelper;
import com.yvan.leto.server.domain.Segment;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

@Service
public class ConfigParserService implements ApplicationContextAware {

    public static final String PARSER_PREFIX = "Parser_";

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public IParser getParser(Segment segment) {
        if (SegmentHelper.SPRING_BOOT.equals(segment.getName())) {
            return applicationContext.getBean(ConfigParserService.PARSER_PREFIX + SegmentHelper.SPRING_BOOT, IParser.class);
        }

        if (SegmentHelper.LOGBACK.equals(segment.getName())) {
            return applicationContext.getBean(ConfigParserService.PARSER_PREFIX + SegmentHelper.LOGBACK, IParser.class);
        }

        if (SegmentHelper.YAML.equals(segment.getType())) {
            return applicationContext.getBean(ConfigParserService.PARSER_PREFIX + SegmentHelper.YAML, IParser.class);
        }

        if (SegmentHelper.PROP.equals(segment.getType())) {
            return applicationContext.getBean(ConfigParserService.PARSER_PREFIX + SegmentHelper.PROP, IParser.class);
        }

        return null;
    }

}
